<h1><?php echo $this->pageTitle = __d('lil_ads', 'Daily Statistics', true); ?></h1>
<div class="index">
	<?php 
		echo $this->Html->script('/lil_ads/js/datepicker', false);
		echo $this->Html->css('/lil_ads/css/datepicker', false);
		
		if (empty($params['banner_id'])) {
			echo '<h2>' . $this->Html->clean($campaign['AdsCampaign']['title']) . '</h2>';
			foreach ($campaign['AdsBanner'] as $banner) {
				echo '<div style="margin-left: 20px">';
				echo $this->Html->link(
					'#' . $banner['id'] . ' ' . $banner['title'],
					array_merge($params, array('banner_id' => $banner['id']))
				);
				echo '</div>';
			}
		} else {
			echo '<div style="margin-left: 12px">';
			echo $this->Html->link(
				$campaign['AdsCampaign']['title'],
				array_merge($params, array('banner_id'=>null))
			);
			echo '</div>';
			foreach ($campaign['AdsBanner'] as $banner) {
				if ($banner['id'] == $params['banner_id']) {
					echo '<h3>#' . $banner['id'] . ' ' . $this->Html->clean($banner['title']) . '</h3>';
				} else {
					echo '<div style="margin-left: 20px">';
					echo $this->Html->link(
						'#' . $banner['id'] . ' ' . $banner['title'],
						array_merge($params, array('banner_id' => $banner['id']))
					);
					echo '</div>';
				}
			}
		}
	?>
	<br />
	<script type="text/javascript">
		function SetDatePopup(link, id) {
			if (!id) id = 'available';
			var picked = datePickerController.datePickers[id].date;

			var d  = (picked.getDate() < 10) ? "0" + picked.getDate() : picked.getDate();
			var m  = ((picked.getMonth() + 1) < 10) ? "0" + (picked.getMonth() + 1) : picked.getMonth() + 1;
			document.location.href = link.replace(/\%d/, picked.getFullYear()+'-'+m+'-'+d);
		};
	</script>
	<div class="filter"> 
		<?php 
			__d('lil_ads', 'Dates from'); echo ' ';
			echo $this->Html->link(
				strftime(Configure::read('LilAds.dateShort'), strtotime($params['start'])),
				'javascript:return void(0);',
				array('id' => 'fd-but-start')
			);
			echo ' '.__d('lil_ads', 'to', true).' ';
			echo $this->Html->link(
				strftime(Configure::read('LilAds.dateShort'), strtotime($params['end'])),
				'javascript:return void(0);',
				array('id' => 'fd-but-end')
			); 
		?>.
	</div>
	<?php
		printf(
			'<input type="hidden" name="start" id="start" value="%1$s" class="format-y-m-d divider-dash no-transparency" onchange="%2$s" />',
			$this->Html->clean($params['start']),
			'SetDatePopup(\'' . $this->Html->url(array_merge($params, array('start' => '%d'))) . '\', \'start\');'
		);
		printf(
			'<input type="hidden" name="end" id="end" value="%1$s" class="format-y-m-d divider-dash no-transparency" onchange="%2$s" />',
			$this->Html->clean($params['end']),
			'SetDatePopup(\'' . $this->Html->url(array_merge($params, array('end' => '%d'))) . '\', \'end\');'
		);
	?>
	<table cellspacing="0" cellpadding="0">
		<tr>
			<th align="center"><?php __d('lil_ads', 'Date'); ?></th>
			<th align="center"><?php __d('lil_ads', 'Click Count'); ?></th>
			<th align="center"><?php __d('lil_ads', 'Display Count'); ?></th>
			<th align="center"><?php __d('lil_ads', 'CTR [%]'); ?></th>
		</tr>
		<?php
			$i = 0; $sumClicks = 0; $sumViews = 0;
						
			$start_day = strtotime($params['start']);
			$end_day = strtotime($params['end']);
			 
			reset($stats); $st = current($stats);
			for ($i = $start_day; $i <= $end_day; $i += 24*60*60) {
				if (isset($st[0]['date_hour']) && strftime('%Y-%m-%d', strtotime($st[0]['date_hour'])) == strftime('%Y-%m-%d', $i)) {
					$clicks = $st[0]['clicks'];
					$views = $st[0]['views'];
					$ctr = sprintf('%.2f', ($st[0]['views'] == 0) ? 0 : ($st[0]['clicks'] / $st[0]['views'] * 100));
					$sumClicks += $st[0]['clicks'];
					$sumViews += $st[0]['views'];
					$st = next($stats);
				} else {
					$clicks = 0;
					$views = 0;
					$ctr = 0;
				}
		?>
		<tr<?php echo (((int)($i / (24*60*60)) % 2 == 0) ? ' class="altrow"' : ''); ?>>
			<td align="center">
			<?php
				echo $this->Html->link(
					strftime(Configure::read('LilAds.dateShort'), $i),
					array_merge(
						array_intersect_key(
							$params,
							array('campaign_id' => '', 'banner_id' => '')
						),
						array('action' => 'admin_hourly', 'date' => strftime('%Y-%m-%d'))
					)
				); ?></td>
			<td align="center"><?php echo $clicks; ?></td>
			<td align="center"><?php echo $views; ?></td>
			<td align="center"><?php echo $ctr; ?></td>
		</tr>
		<?php
			}
		?>
		<tr>
			<th align="center">SUM </th>
			<th align="center"><?php echo $sumClicks; ?></th>
			<th align="center"><?php echo $sumViews; ?></th>
			<th align="center"><?php printf('%.2f', ($sumViews==0) ? 0 : ($sumClicks / $sumViews * 100)); ?></th>
		</tr>
	</table>
</div>